package com.sgcc.pda.mdrh.zoom.router;

import android.content.Context;
import android.os.RemoteException;

import com.google.gson.Gson;
import com.sgcc.pda.baselibrary.utils.LogUtil;
//import com.sgcc.pda.greendao.entity.Msg;
//import com.sgcc.pda.greendao.entity.OfflineMsg;
//import com.sgcc.pda.greendao.entity.SubscribeInfo;
//import com.sgcc.pda.greendao.model.OfflineMsgModel;
//import com.sgcc.pda.greendao.model.SubscribeInfoModel;
import com.sgcc.pda.hzaidl.entity.CommonErrorBean;
import com.sgcc.pda.hzaidl.entity.RequestBean;
import com.sgcc.pda.model.web.request.BlockUploadFileRequest;
import com.sgcc.pda.model.web.request.CancelBlockUploadFileRequest;
import com.sgcc.pda.model.web.request.CommParmUpdateRequest;
import com.sgcc.pda.model.web.request.DownloadFileRequest;
import com.sgcc.pda.model.web.request.DownloadPicFileRequest;
import com.sgcc.pda.model.web.request.GetOfflineMsgRequest;
import com.sgcc.pda.model.web.request.PostDataCallbackRequest;
import com.sgcc.pda.model.web.request.PublishRequest;
import com.sgcc.pda.model.web.request.ReportReceivedMsgRequest;
import com.sgcc.pda.model.web.request.SubscribeRequest;
import com.sgcc.pda.model.web.request.UnsubscribeRequest;
import com.sgcc.pda.model.web.request.UploadFileRequest;
import com.sgcc.pda.model.web.response.SingleBooleanResponse;
import com.sgcc.pda.sdk.utils.NetWorkUtil;
import com.sgcc.pda.ssal.mqtt.MQTTManager;
import com.sgcc.pda.ssal.service.SsalWebControler;
import com.sgcc.pda.web.impl.HttpCallbackInterface;

import java.util.List;

/**
 * Created by cj on 2019/12/2.
 */
public class SsalRouter extends BaseOperateRouter {

    @Override
    public String handleResult(Context context, final RequestBean requestBean, final CompleteListener completeListener) {
        String methodName = requestBean.getMethodName();
        switch (methodName) {
            case "postDataCallback":
                try {
                    PostDataCallbackRequest postDataCallbackRequest = new Gson().fromJson(requestBean.getParamJson(), PostDataCallbackRequest.class);
                    SsalWebControler.getInstance().postDataCallback(context, postDataCallbackRequest.getUri(), postDataCallbackRequest.getRequestParams(), new HttpCallbackInterface() {
                        @Override
                        public void httpSuccess(String result) {

                            completeListener.complete(1, result);
                            LogUtil.e("zh", result);
                        }

                        @Override
                        public void httpFailed(int errorCode, String errorMsg) {

                            CommonErrorBean commonErrorBean = new CommonErrorBean();
                            commonErrorBean.setCode(errorCode);
                            commonErrorBean.setMsg(errorMsg);
                            String s = new Gson().toJson(commonErrorBean);
                            completeListener.complete(0, s);
                            LogUtil.e("zh", s);

                        }
                    });
                } catch (Exception e) {
                    if ("com.sgcc.pda.gis".equals(requestBean.getPackageName())) {
                        LogUtil.e("测试路由接口回调(web)Failed:", e.getLocalizedMessage());
                    }

                    e.printStackTrace();

                }
                break;

            case "downloadFile":
//                WebControler.getInstance().downloadFile();

                DownloadFileRequest downloadFileRequest = new Gson().fromJson(requestBean.getParamJson(), DownloadFileRequest.class);
                SsalWebControler.getInstance().downloadComm(context, downloadFileRequest.getUri(), downloadFileRequest.getType(), downloadFileRequest.getFileName(), downloadFileRequest.getFilePath(), completeListener);

                break;

            case "downloadPicFile":
                //TODO: 未适配ssal - 2020-06-03
                // 下载文件接口，支持断点续传，参数是文件路径
                DownloadPicFileRequest picFileRequest = new Gson().fromJson(requestBean.getParamJson(), DownloadPicFileRequest.class);
                SsalWebControler.getInstance().downloadPicFile(context, picFileRequest.getFilePath(), picFileRequest.getSavePath(), completeListener);
                break;

            case "uploadFile":

                UploadFileRequest uploadFileRequest = new Gson().fromJson(requestBean.getParamJson(), UploadFileRequest.class);
                SsalWebControler.getInstance().uploadFile(context, uploadFileRequest.getUri(), uploadFileRequest.getRequestParams(), uploadFileRequest.getFilePath(), uploadFileRequest.getFileKey(), completeListener);

                break;

            case "commParmUpdate":
                //TODO: 未适配ssal - 2020-06-03
                // 主程序和业务APP的【参数更新】接口
                CommParmUpdateRequest commParmUpdateRequest = new Gson().fromJson(requestBean.getParamJson(), CommParmUpdateRequest.class);
                SsalWebControler.getInstance().commParmUpdate(context, commParmUpdateRequest.getAppIdentity(), commParmUpdateRequest.getParamVersion(), new HttpCallbackInterface() {
                    @Override
                    public void httpSuccess(String result) {
                        completeListener.complete(1, result);
                    }

                    @Override
                    public void httpFailed(int errorCode, String errorMsg) {
                        CommonErrorBean commonErrorBean = new CommonErrorBean();
                        commonErrorBean.setCode(errorCode);
                        commonErrorBean.setMsg(errorMsg);
                        String s = new Gson().toJson(commonErrorBean);
                        completeListener.complete(0, s);
                    }
                });
                break;

            case "blockUploadFile":
                // 分块上传文件
                BlockUploadFileRequest blockUploadRequest = new Gson().fromJson(requestBean.getParamJson(), BlockUploadFileRequest.class);
                SsalWebControler.getInstance().blockUploadFile(blockUploadRequest.getFilePath(), blockUploadRequest.getSavePath(), blockUploadRequest.isDelete(), completeListener);
                break;
            case "cancelBlockUploadFile":
                // 取消分块上传文件
                CancelBlockUploadFileRequest cancelBlockUploadRequest = new Gson().fromJson(requestBean.getParamJson(), CancelBlockUploadFileRequest.class);
                SsalWebControler.getInstance().cancelBlockUploadFile(cancelBlockUploadRequest.getFilePath(), cancelBlockUploadRequest.getSavePath(), completeListener);
                break;

            case "subscribe":

                return subscribe(context, requestBean);

            case "unsubscribe":

                return unsubscribe(context, requestBean);

            case "publish":

                return publish(context, requestBean);

            case "getOfflineMsg":
                /*TODO
                GetOfflineMsgRequest getOfflineMsgRequest = new Gson().fromJson(requestBean.getParamJson(), GetOfflineMsgRequest.class);
                String packageName = getOfflineMsgRequest.getPackageName();
                OfflineMsgModel offlineMsgModel = new OfflineMsgModel();
                OfflineMsg offlineMsg = offlineMsgModel.queryByKey(packageName);
                if (offlineMsg == null) return "";
                List<Msg> msgList = offlineMsg.getMsgList();
                if (msgList.size() == 0) return "";
                return new Gson().toJson(msgList); */

            case "reportReceivedMsg":
                /*TODO
                ReportReceivedMsgRequest reportReceivedMsgRequest = new Gson().fromJson(requestBean.getParamJson(), ReportReceivedMsgRequest.class);
                String packageName1 = reportReceivedMsgRequest.getPackageName();
                String time = reportReceivedMsgRequest.getTime();

                OfflineMsgModel model = new OfflineMsgModel();
                model.deleteOfflineMsg(packageName1, time);
                return new Gson().toJson(new SingleBooleanResponse(true));*/
            case "getKnows":

                PostDataCallbackRequest postDataCallbackRequest = new Gson().fromJson(requestBean.getParamJson(), PostDataCallbackRequest.class);
                try {
                    SsalWebControler.getInstance().getKnows(context, postDataCallbackRequest.getUri(), "", postDataCallbackRequest.getRequestParams(), new HttpCallbackInterface() {
                        @Override
                        public void httpSuccess(String result) {

                            completeListener.complete(1, result);
                            LogUtil.e("zh", result);
                        }

                        @Override
                        public void httpFailed(int errorCode, String errorMsg) {

                            CommonErrorBean commonErrorBean = new CommonErrorBean();
                            commonErrorBean.setCode(errorCode);
                            commonErrorBean.setMsg(errorMsg);
                            String s = new Gson().toJson(commonErrorBean);
                            completeListener.complete(0, s);
                            LogUtil.e("zh", s);
                        }
                    });
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                break;
            default:
                break;

        }
        return null;
    }

    /**
     * 订阅主题
     */
    private String subscribe(Context context, RequestBean requestBean) {
        /*TODO
        boolean connected = NetWorkUtil.getInstance().isConnected(context);
        if (!connected) return new Gson().toJson(new SingleBooleanResponse(false));
        SubscribeRequest subscribeRequest = new Gson().fromJson(requestBean.getParamJson(), SubscribeRequest.class);
        String packageName = subscribeRequest.getPackageName();
        String topic = subscribeRequest.getTopic();
        SubscribeInfoModel model = new SubscribeInfoModel();
        boolean isExist = model.isExist(topic, packageName);
        if (!isExist) {
            model.save(topic, packageName);
        }
        boolean subscribe = MQTTManager.getInstance().subscribe(topic);
        return new Gson().toJson(new SingleBooleanResponse(subscribe));*/
        return "";
    }

    /**
     * 取消订阅
     */

    private String unsubscribe(Context context, RequestBean requestBean) {
        /*TODO
        boolean connected = NetWorkUtil.getInstance().isConnected(context);
        if (!connected) return new Gson().toJson(new SingleBooleanResponse(false));

        UnsubscribeRequest unsubscribeRequest = new Gson().fromJson(requestBean.getParamJson(), UnsubscribeRequest.class);
        String packageName = unsubscribeRequest.getPackageName();
        String topic = unsubscribeRequest.getTopic();

        SubscribeInfoModel subscribeInfoModel = new SubscribeInfoModel();
        subscribeInfoModel.deleteApp(topic, packageName);
        SubscribeInfo subscribeInfo = subscribeInfoModel.queryByKey(topic);
        if (subscribeInfo != null) {
            List<String> packageList = subscribeInfo.getPackageList();
            if (packageList.size() == 0) {
                //最后一个订阅取消订阅的时候，才去调用真正的取消订阅接口
                boolean result = MQTTManager.getInstance().unsubscribe(topic);
                return new Gson().toJson(new SingleBooleanResponse(result));
            }
        }
        return new Gson().toJson(new SingleBooleanResponse(true));*/
        return "";
    }

    /**
     * 发布消息
     */
    private String publish(Context context, RequestBean requestBean) {
        boolean connected = NetWorkUtil.getInstance().isConnected(context);
        if (!connected) return new Gson().toJson(new SingleBooleanResponse(false));

        PublishRequest publishRequest = new Gson().fromJson(requestBean.getParamJson(), PublishRequest.class);
        String content = publishRequest.getContent();
        String topic = publishRequest.getTopic();
        boolean result = MQTTManager.getInstance().publish(topic, content);
        return new Gson().toJson(new SingleBooleanResponse(result));
    }
}
